/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.saville.mc;

import com.saville.debug.Log;
import com.saville.msgcomp.*;

/*
 * A Msg component implemented in "pure Java"
 */
abstract public class Mc extends McBase {
    
    /*
     * Constructor
     */
    public Mc() {
    }

    /*
     * Constructor
     */
    public Mc(String name, MsgPump msgPump, boolean doSendCmdStarted) {
        super(name, msgPump, doSendCmdStarted);
    }

    /*
     * Constructor
     */
    public Mc(String name, MsgPump msgPump) {
        this(name, msgPump, true);
    }

    /*
     * Constructor
     */
    public Mc(String name) {
        this(name, MsgPump.myMsgPump(), true);
    }
    
    /*
     * Implement abstract init from McBase
     */
    @Override
    public void init(String name, MsgPump msgPump, boolean doSendCmdStarted) {
        Log.d(df, "Mc: %s init() E", name);

        mName = name;

        // If looper is null a runtime exception will be thrown
        if (msgPump == null) {
            throw new RuntimeException("Mc#init: looper is null, Looper.prepare was not called");
        }

        mMsgPump = msgPump;

        // Register ourselves
        mMcId = mMcMgr.register(mName, this);

        if (doSendCmdStarted) {
            sendCmdStarted();
        }

        Log.d(df, "Mc: %s init() X", mName);
    }

    /*
     * Implement abstract queueMsg from McBase/IMc
     */
    @Override
    public void queueMsg(Msg msg) {
        msg.handler = this;
        mMsgPump.queueMsg(msg);
    }
}
